<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>inStorage</title>
<link rel="stylesheet" type="text/css" href="../css/default.css"/>
<link rel="stylesheet" type="text/css" href="../css/css.css"/>
<link rel="stylesheet" type="text/css"
      href="../jquery/redmond/jquery-ui-1.9.0.custom.min.css"/>
<script type="text/javascript" src="../jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript"
        src="../jquery/jquery-ui-1.9.0.custom.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-extends.js"></script>
<script type="text/javascript" src="../jquery/jquery.printElement.min.js"></script>
<script>
/**
 * 绑定tab点击事件
 */
function bindTabAction() {
    $("li#inStorageTab").click(function () {
        $("div#inStatusBar").html("");
        requestData("", "0");
    });
    $("li#cancelStorageTab").click(function () {
        $("div#cancelStatusBar").html("");
        requestData("", "1");
    });
}

/**
 * 绑定按钮点击事件
 */
function bindBtnAction() {
    $("button#inStorageBtn").click(function () {
        var jsonSent = "{'bs':[";
        var validation = false;
        $("td input:checked.inStorageAllCheckClass").each(function (i, e) {
            jsonSent = jsonSent + "'" + $(this).attr("value") + "',";
            validation = true;
        });
        jsonSent = jsonSent.substring(0, jsonSent.length - 1);
        jsonSent = jsonSent + "]}";
        if (validation) {
            $.post("../../storage/instorage.htm",
                    {"json":jsonSent},
                    function (r) {
                        if (r.result == "1") {
//                            $("div#inStatusBar").html("入库成功");
                            requestData($("input#batchId").attr("value"), "0");
                        } else {
                            $("div#inStatusBar").html(r.err);
                        }

                    },
                    "json");
        } else {
            $.alert("没有选择任何单号", "提示");
        }

    });
    $("button#cancelStorageBtn").click(function () {
        var jsonSent = "{'bs':[";
        var validation = false;
        $("td input:checked.cancelStorageAllCheckClass").each(function (i, e) {
            jsonSent = jsonSent + "'" + $(this).attr("value") + "',";
            validation = true;
        });
        jsonSent = jsonSent.substring(0, jsonSent.length - 1);
        jsonSent = jsonSent + "]}";
        if (validation) {
            $.post("../../storage/cancelstorage.htm",
                    {"json":jsonSent},
                    function (r) {
                        if (r.result == "1") {
//                            $("div#cancelStatusBar").html("撤单成功");
                            requestData($("input#cancelBatchId").attr("value"), "1");
                        } else {
                            $("div#cancelStatusBar").html(r.err);
                        }

                    },
                    "json");
        } else {
            $.alert("没有选择任何单号", "提示");
        }


    });


    $("button#search").click(function () {
        requestData($("input#batchId").attr("value"), "0");
        $("div#inStatusBar").html("");
    });
    $("button#searchCancel").click(function () {
        requestData($("input#cancelBatchId").attr("value"), "1");
        $("div#cancelStatusBar").html("");
    });


    $("input#inStorageAllCheck").click(function () {
        if ($(this).attr("checked") == 'checked') {
            $("input:checkbox.inStorageAllCheckClass").attr("checked", "");
        } else {
            $("input:checkbox.inStorageAllCheckClass").removeAttr("checked");
        }
    });

    $("input#outStorageAllCheck").click(function () {
        if ($(this).attr("checked") == 'checked') {
            $("input:checkbox.cancelStorageAllCheckClass").attr("checked", "");
        } else {
            $("input:checkbox.cancelStorageAllCheckClass").removeAttr("checked");
        }
    });


}

/**
 * 绑定键盘回车事件
 */
function bindKeyAction() {
    $("input#cancelBatchId").keydown(function (e) {
        if (e.which == 13) {
            requestData($(this).attr("value"), "1");
            $("div#cancelStatusBar").html("");
        }

    });
    $("input#batchId").keydown(function (e) {
        if (e.which == 13) {
            requestData($(this).attr("value"), "0");
            $("div#inStatusBar").html("");
        }
    });
}

/**
 * 请求数据填充表格
 * @param searchStr  查询字符串
 * @param type  查询类型  0 查询未入库  1 查询已入库
 */
function requestData(searchStr, type) {

    var html;
    if (type == "0") {
        $("table#inStorageList").html("");
        html = "<thead>"
                + "<tr>"
                + "<th width='90' style='text-align: left'><input type='checkbox' id='inStorageCheckAll' class='inStorageAllCheckClass'/><label for='inStorageCheckAll'>全选</label></th>"
                + "<th>未入库单号</th>"
                + "<th>生产完成时间</th>"
                + "<th width='200'>总箱数</th>"
                + "<th width='100'>操作</th>"
                + "</tr>"
                + "</thead>";
        $("table#inStorageList").append(html);
    } else {
        $("table#cancelStorageList").html("");
        html = "<thead>"
                + "<tr>"
                + "<th width='90' style='text-align: left'><input type='checkbox' id='cancelStorageCheckAll' class='cancelStorageAllCheckClass'/><label for='cancelStorageCheckAll'>全选</label></th>"
                + "<th>已入库单号</th>"
                + "<th>生产完成时间</th>"
                + "<th>入库时间</th>"
                + "<th width='200'>总箱数</th>"
                + "<th width='100'>操作</th>"
                + "</tr>"
                + "</thead>";
        $("table#cancelStorageList").append(html);
    }

    $("th input:checkbox.inStorageAllCheckClass").click(function () {
        if ($(this).attr("checked") == 'checked') {
            $("input:checkbox.inStorageAllCheckClass").attr("checked", "");
        } else {
            $("input:checkbox.inStorageAllCheckClass").removeAttr("checked");
        }
    });

    $("th input:checkbox.cancelStorageAllCheckClass").click(function () {
        if ($(this).attr("checked") == 'checked') {
            $("input:checkbox.cancelStorageAllCheckClass").attr("checked", "");
        } else {
            $("input:checkbox.cancelStorageAllCheckClass").removeAttr("checked");
        }
    });


    var url;
    if (type == "0") {
        url = "../../storage/inlist.htm";
    } else if (type == "1") {
        url = "../../storage/cancelquery.htm";
    }


    $.ajax({
        url:url,
        type:"POST",
        cache:false,
        dataType:"json",
        data:{"batchId":searchStr},
        success:function (data, textStatus, jqXHR) {

            if (data != null && data != "") {
                if (data.result == "1") {
                    var storageInTime;
                    var html;
                    for (var i = 0; i < data.bs.length; i++) {
                        var batchId = data.bs[i].b;
                        if (type == "0") {
                            html = "<tr id='listData'><td align='left'><input type='checkbox' class='inStorageAllCheckClass' value='"
                                    + batchId
                                    + "'></td><td id='"
                                    + batchId
                                    + "' align='center'>"
                                    + batchId
                                    + "</td>"
                                    + "<td align='center'>"
                                    + data.bs[i].pdtm
                                    + "</td><td align='center'>"
                                    + data.bs[i].num
                                    + "</td><td align='center'><button class='" + batchId + "'>入库</button></td>"
                                    + "</tr>";
                            $("#inStorageList").append(html);
                            $("td#" + batchId).next().next().next().children("button").click(function () {
                                var jsonSent = "{'bs':['" + $(this).attr("class") + "']}";

                                $.post("../../storage/instorage.htm",
                                        {"json":jsonSent},
                                        function (r) {
                                            if (r.result == "1") {
                                                requestData($("input#batchId").attr("value"), "0");
                                            } else {
                                                $("div#inStatusBar").html(r.err);
                                            }
                                        },
                                        "json");

                            });


                        } else if (type == "1") {
                            storageInTime = data.bs[i].sitm;
                            html = "<tr id='listData'><td align='left'><input type='checkbox' class='cancelStorageAllCheckClass' value='"
                                    + batchId
                                    + "'></td><td id='"
                                    + batchId
                                    + "' align='center'>"
                                    + batchId
                                    + "</td><td align='center'>"
                                    + data.bs[i].pdtm
                                    + "</td><td align='center'>"
                                    + storageInTime
                                    + "</td><td align='center'>"
                                    + data.bs[i].num
                                    + "</td><td align='center'><button class='" + batchId + "'>撤单</button></td>"
                                    + "</tr>";
                            $("#cancelStorageList").append(html);

                            $("td#" + batchId).next().next().next().next().children("button").click(function () {
                                var jsonSent = "{'bs':['" + $(this).attr("class") + "']}";

                                $.post("../../storage/cancelstorage.htm",
                                        {"json":jsonSent},
                                        function (r) {
                                            if (r.result == "1") {
                                                requestData($("input#cancelBatchId").attr("value"), "1");
                                            } else {
                                                $("div#cancelStatusBar").html(r.err);
                                            }

                                        },
                                        "json");
                            });


                        }


                        $("td#" + batchId).hover(function () {
                            $(this).css("cursor", "pointer");
                        });
                        $("td#" + batchId).click(function () {
                            var batchId = $(this).attr("id");
                            $.post("../../storage/getbatchdetail2.htm",
                                    {"batchId":batchId},
                                    function (r) {
                                        if (r.result == "1") {

                                            $("p#firstp").html("单号 : " + r.b);
                                            $("p#secondp").html("生产完成时间 : " + r.ft + "&nbsp;&nbsp;入库时间 : " + r.sitm);

                                            var html = "";
                                            for (var i = 0; i < r.ps.length; i++) {
                                                html = html + "<tr id='" + r.ps[i].pid + "' class='datarow'><td>"
                                                        + r.ps[i].nc
                                                        + "</td><td>"
                                                        + r.ps[i].pdn
                                                        + "</td><td>"
                                                        + r.ps[i].num
                                                        + "</td>"
                                                        + "</tr><tr style='display: none'><td colspan='3'>"
                                                        + "箱号:<br/>";
                                                for (var j = 0; j < r.ps[i].boxes.length; j++) {
                                                    if (j != (r.ps[i].boxes.length - 1)) {
                                                        html = html + r.ps[i].boxes[j].ids2e + ","
                                                    } else {
                                                        html = html + r.ps[i].boxes[j].ids2e;
                                                    }
                                                }
                                                html = html + "</td></tr>"

                                            }

                                            $("tbody#statics").html(html);

                                            $(".datarow").hover(function () {
                                                $(this).css("cursor", "pointer");
                                            });

                                            for (var i = 0; i < r.ps.length; i++) {
                                                $("tr#" + r.ps[i].pid).click(function () {
                                                    if ($(this).next("tr").css("display") == "none") {
                                                        $(this).next("tr").css("display", "");
                                                    } else {
                                                        $(this).next("tr").css("display", "none");
                                                    }

                                                });


                                            }

                                            $("p#totalnum").html("总箱数 : " + r.sum);


                                            $("div#dialog").dialog({
                                                autoOpen:false,
                                                height:800,
                                                width:1000,
                                                modal:true,
                                                close:function () {
                                                }
                                            });


                                            $("div#dialog")
                                                    .dialog(
                                                    "option",
                                                    {
                                                        buttons:{
                                                            "取消":function () {
                                                                $(this).dialog("close");
                                                            },
                                                            "打印":function () {
                                                                var el = document
                                                                        .getElementById('printBlock');
                                                                var iframe = document
                                                                        .createElement('IFRAME');
                                                                var doc = null;
                                                                iframe
                                                                        .setAttribute('style',
                                                                        'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
                                                                document.body.appendChild(iframe);
                                                                doc = iframe.contentWindow.document;

                                                                doc
                                                                        .write('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
                                                                        + '<link rel="stylesheet" type="text/css"	href="../jquery/redmond/jquery-ui-1.9.0.custom.min.css" />'
                                                                        + '<link rel="stylesheet" type="text/css" href="../css/default.css" />'
                                                                        + '<link rel="stylesheet" type="text/css" href="../css/print.css" />'
                                                                        + '<style>@media print{button{display:none} tr{display:block}}</style>');

                                                                doc.write(el.innerHTML);
                                                                doc.close();

                                                                iframe.contentWindow.focus();
                                                                iframe.contentWindow.print();
                                                                if (navigator.userAgent.indexOf("MSIE") > 0) {
                                                                    document.body.removeChild(iframe);
                                                                }

//                                                                $("div#printBlock").printElement({
//                                                                    overrideElementCSS:['../css/default.css',
//                                                                        '../jquery/redmond/jquery-ui-1.9.0.custom.min.css',
//                                                                        '../css/print.css']
//                                                                });

                                                            }
                                                        }
                                                    });
                                            $("div#dialog").dialog("open");

                                        } else {
                                            $.alert(r.err);
                                        }

                                    },
                                    "json");


                        });
                        $("td button").click(function () {
                            $(this).parents("tr").remove();
                            $('table tr#listData').removeClass('hover');
                            $('table tr#listData:even').addClass('hover');
                        });

                    }

                    $('table tr').removeClass('hover');
                    $('table tr:even').addClass('hover');


                } else {
                    //display error
                    if (type == "0") {
                        $("div#inStatusBar").html(data.err);
                    } else {
                        $("div#cancelStatusBar").html(data.err);
                    }

                }
            }
        }
    });


}

/**
 * 初始化
 */
$(function () {
    if ($.browser.msie) {
        $("#tabs").tabs({
            activate:function (event, ui) {
                var oselector = ui.oldPanel.selector + '';
                var nselector = ui.newPanel.selector + '';
                var oid = oselector.substring(oselector.length - 1);
                var nid = nselector.substring(nselector.length - 1);
                $("#tabs-" + oid).hide();
                $("#tabs-" + nid).show();
            }
        });
    } else {
        $("#tabs").tabs();
    }

    bindBtnAction();
    bindKeyAction();
    bindTabAction();


    $("div#dialog").dialog({
        autoOpen:false,
        height:1000,
        width:800,
        modal:true,
        close:function () {
        }
    });


    if (!window.parent.isGranted(3)) {
        $("li#cancelStorageTab").hide();
    }
    if (!window.parent.isGranted(1)) {
        $("li#inStorageTab").hide();

    }
    var tabs = "";
    if (window.parent.isGranted(0)) {
        tabs = "1";
    } else if (window.parent.isGranted(1)) {
        tabs = "1";
    } else if (window.parent.isGranted(3)) {
        tabs = "2";
    }

    if (tabs == "1") {
        $("div#tabs-2").hide();
        $("div#tabs-1").show();
        requestData("", "0");
    } else if (tabs == "2") {
        $("div#tabs-2").show();
        $("div#tabs-1").hide();
        requestData("", "1");
    }
});
</script>
</head>
<body>
<div id="tabs">
    <ul>
        <li id="inStorageTab"><a href="#tabs-1">入库</a></li>
        <li id="cancelStorageTab"><a href="#tabs-2">撤单</a></li>
    </ul>
    <div id="tabs-1">
        <div id="inStorageBar" style="padding: 5px;">
            <label for="batchId">单号:</label><input id="batchId" title="请输入您要查询的未入库单号"/>
            <button id="search">搜索</button>
        </div>

        <div id="inStatusBar" style="color: red;background-color: yellow;"></div>

        <table id="inStorageList" cellpadding="0" cellspacing="0">
        </table>

        <div style="padding: 5px;" align="right">
            <input type='checkbox' id='inStorageAllCheck' class='inStorageAllCheckClass'><label for='inStorageAllCheck'>全选</label>
            <button id="inStorageBtn" title="点击后列表中勾选的未入库单入库">批量入库</button>
        </div>
    </div>
    <div id="tabs-2">
        <div id="cancelStorageBar" style="padding: 5px;">
            <label for="cancelBatchId">撤销单号:</label><input id="cancelBatchId" title="请输入您要撤销的已入库单号"/>
            <button id="searchCancel">搜索</button>
        </div>

        <div id="cancelStatusBar" style="color: red;background-color: yellow;"></div>

        <table id="cancelStorageList" cellpadding="0" cellspacing="0">
        </table>

        <div style="padding: 5px;" align="right">
            <input type='checkbox' id='outStorageAllCheck' class='cancelStorageAllCheckClass'><label
                for='outStorageAllCheck'>全选</label>
            <button id="cancelStorageBtn" title="点击后列表中的单据撤销">批量撤单</button>
        </div>
    </div>

</div>


<div id="dialog" title="入库单">
    <div id='printBlock'>
        <h3 align='center'>入库单</h3>

        <p id='firstp' align='left'></p>

        <p id='secondp' align='left'></p>
        <table class='instorageListClass'
               style="border: solid #2575B4; font-family: ''; font-size: 1em; height: auto">
            <thead>
            <tr style="border: solid #2575B4">
                <th width="33%">产品代码</th>
                <th>产品名称</th>
                <th width="33%">箱数</th>
            </tr>
            </thead>
            <tbody id="statics">
            </tbody>
        </table>
        <p id="totalnum" align='right'></p>

        <p align='right'><span style='padding-right: 80px'>经手人:</span><span style='padding-right: 60px'>保管人:</span></p>
    </div>
</div>


</body>
</html>